package ar.Location.Weather;

import java.io.InputStream;
import java.net.URLEncoder;

import javax.xml.parsers.SAXParserFactory;

import android.util.Xml;
import ar.Location.Helper.HttpProvider;

public class WeatherProvider {
	
	public static String urlBaseRequest = "https://www.google.com/ig/api?weather=";
	
	public static Weather getWeatherForAddress(String address) {
		if(address != null && address.length() > 0) {
			try {
				address = URLEncoder.encode(address, "UTF-8");
				String urlRequest = urlBaseRequest + address;
				SAXParserFactory spf = SAXParserFactory.newInstance();
				spf.setValidating(false);
				InputStream inputStream = HttpProvider
						.getInputStream(urlRequest);
				WeatherHandler weatherHandler = new WeatherHandler();
				Xml.parse(inputStream, Xml.Encoding.UTF_8, weatherHandler);
				return weatherHandler.getWeather();
			} catch (Exception e) {
				// TODO: handle exception
				return null;
			}
		}
		return null;
	}
}
